package com.ruoyi.employee.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.dto.TbEmployeeDto;
import com.ruoyi.employee.service.TbEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/employee/")
public class EmployeeController {
    @Autowired
    private TbEmployeeService tbEmployeeService;

    @RequestMapping("pageemployee")
    public AjaxResult pageEmployee(@RequestBody TbEmployeeDto tbEmployeeDto) {
        Page<TbEmployeeDto> page = new Page<>(tbEmployeeDto.getPageNum(),tbEmployeeDto.getPageSize());
        IPage<TbEmployeeDto> iPage = tbEmployeeService.pageEmployee(page,tbEmployeeDto);
        return AjaxResult.success(iPage);
    }


    @PostMapping("addemployee")
    public AjaxResult addEmployee(@RequestBody TbEmployeeDto tbEmployeeDto) {
        tbEmployeeService.addEmployee(tbEmployeeDto);
        return AjaxResult.success(tbEmployeeDto);
    }

    @PostMapping("blockupemployee")
    public AjaxResult blockupEmployee(@RequestBody TbEmployeeDto tbEmployeeDto) {
        Integer i = tbEmployeeService.blockupEmployee(tbEmployeeDto);
        if(i == 1) {
            return AjaxResult.success("修改成功！！！");
        }
        return AjaxResult.error("修改失败！！！");
    }

    @GetMapping("getEmployeeById/{id}")
    public AjaxResult getEmployeeById(@PathVariable Long id) {
        TbEmployeeDto tbEmployeeDto = tbEmployeeService.getEmployeeById(id);
        return AjaxResult.success(tbEmployeeDto);
    }

    @PostMapping("updateEmployee")
    public AjaxResult updateEmployee(@RequestBody TbEmployeeDto tbEmployeeDto){
        tbEmployeeService.updateEmployee(tbEmployeeDto);
        return AjaxResult.success("修改成功");
    }

    @GetMapping("deleteEmployee/{employeeId}")
    public AjaxResult deleteEmployee(@PathVariable Long employeeId) {
        Integer i = tbEmployeeService.deletedEmployeeAndOther(employeeId);
        if (i == 1) {
            return AjaxResult.success("删除成功！");
        }
        return AjaxResult.success("删除失败！！！");
    }

//    @GetMapping("contentemployee/{employeeId}")
//    public AjaxResult contentEmployee(@PathVariable Long employeeId) {
//
//    }
}